package com.example.finance_manager.pages.manage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import com.example.finance_manager.R;
import com.example.finance_manager.dao.PayoutDao;
import com.example.finance_manager.dao.SalaryDao;
import com.example.finance_manager.models.Payout;

public class PayoutManageActivity extends AppCompatActivity {
    private EditText etMoney;
    private EditText etOther;
    private EditText etAddress;
    private EditText etTime;
    private EditText etType;
    private Button btnDelete;
    private Button btnCancel;
    private Button btnUpdate;
    private Payout payout;
    private PayoutDao payoutDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_payout);

        payoutDao = new PayoutDao(this);

        initView();
        initData();
        initListener();
    }

    private void initView() {
        etMoney = (EditText) findViewById(R.id.et_manage_payout_money);
        etOther = (EditText) findViewById(R.id.et_manage_payout_other);
        etAddress = (EditText) findViewById(R.id.et_manage_payout_address);
        etTime = (EditText) findViewById(R.id.et_manage_payout_time);
        etType = (EditText) findViewById(R.id.et_manage_payout_type);

        btnDelete = (Button) findViewById(R.id.btn_manage_payout_delete);
        btnCancel = (Button) findViewById(R.id.btn_manage_payout_cancel);
        btnUpdate = (Button) findViewById(R.id.btn_manage_payout_update);
    }

    private void initData() {
        Intent intent = this.getIntent();
        payout = (Payout) intent.getSerializableExtra("payout");
        etMoney.setText(payout.getMoney().toString());
        etOther.setText(payout.getOther());
        etAddress.setText(payout.getAddress());
        etTime.setText(payout.getTime());
        etType.setText(payout.getType());
    }

    private void initListener() {
        btnDelete.setOnClickListener(view -> {
            int res = payoutDao.deleteById(payout.getId());
            System.out.println(res);
            finish();
        });
        btnUpdate.setOnClickListener(view -> {
            String money, other, address, time, type;
            money = etMoney.getText().toString();
            other = etOther.getText().toString();
            address = etAddress.getText().toString();
            time = etTime.getText().toString();
            type = etType.getText().toString();
            Payout _salary = new Payout(time, address, type, other, Double.parseDouble(money));
            _salary.setId(payout.getId());
            System.out.println(_salary);
            payoutDao.update(_salary);
            finish();
        });
        btnCancel.setOnClickListener(view -> {
            finish();
        });
    }
}